<?php
require_once("taskView.php");
require_once(CORE_DIR.'taskModel.php');

class adddailytaskAction extends authAction{

	function __construct(){
		$this->view = new taskView();
	}

	public function run($get, $post, $viewtype) {
		parent::run($get,$post,$viewtype);
		
		$tModel = new taskModel();
		$date = "";
		if (isset($get['date'])) //date format is 2011-03-12
		{
			$strDate = $get['date'];
			$arrDate = explode("-", $strDate);
			if ((count($arrDate) == 3) && (checkdate($arrDate[1], $arrDate[2], $arrDate[0])) )
				$date = $strDate;			
		}
		

		if (isset($get["title"]) || isset($get["id"]))
		{
			if (isset($get["id"]) && isset($get["state"])){
				$tskId = $get["id"];
				$dailyState = $get["state"];

				$status = 0;
				if ($tModel->addDailyTask("", $tskId, $dailyState, $date))
				{
					$status = 1;
					$lastInsertId = $tskId;
				}

			}
			elseif (isset($get["ownerid"]) && isset($get["title"])){
				$tskName = htmlspecialchars($get["title"], ENT_QUOTES);				
				$ownerId = $get["ownerid"];
				$uids = array_merge( array(), (array)$ownerId);
				$lastInsertId = $tModel->addDailyTask($tskName, "", 1, $date);
				$status = 0;
				$id = 0;
			
				if ($lastInsertId > 0){
					$status = 1;
					$id = $lastInsertId;
					$tModel->addUsersToTask($id, $uids, $ownerId);
				}
			}
			else{
				$this->view->showBadCmd();
			}			
			
			$this->view->setProperty("status", $status);
			$this->view->setProperty("id", $lastInsertId);
			$this->view->showAddTask();
		}
		else{
			$this->view->showBadCmd();
		}	
			
	}
}
?>